//
//  SecondViewController.m
//  Test04
//
//  Created by HuHongbing on 9/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BowenmanageViewController.h"
#import "Utility.h"
#import "Config.h"
@interface BowenmanageViewController ()

@end

@implementation BowenmanageViewController



- (void)viewDidLoad{
    [super viewDidLoad];
     self.navigationItem.titleView=[Utility getTitleView:@"博文管理"];
    self.view.backgroundColor = UIColorFromRGB(0xfafafa);
    
    UIImageView *imgeLogo=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 18, 19)];
    imgeLogo.image=[Utility imageNamed:@"cg_quan.png"];
    [self.view addSubview:imgeLogo];
    
    UILabel *labelanthor=[Utility detailTextLabel:CGRectMake(32, 13, 70, 14) andFontSize:12];
    labelanthor.textColor=UIColorFromRGB(0x000000);
    labelanthor.text=@"一起躲猫猫";
    [self.view addSubview:labelanthor];
    
    self.dataArray=[NSMutableArray array];
    [self.dataArray addObject:@{@"logo":@"quanzi_shenhe.png",@"list":@[
                                                                      @"a0"]}];
    [self.dataArray addObject:@{@"logo":@"quanzi_zhiding.png",@"list":@[
     @"b0",@"b1"]}];
    [self.dataArray addObject:@{@"logo":@"quanzi_all.png",@"list":@[
     @"c0",@"c1",@"c3"]}];
    [self.dataArray addObject:@{@"logo":@"quanzi_hide.png",@"list":@[
     @"d0",@"d1",@"d2",@"d3"]}];
    [self loadModel];
    float height=367-38;
    if (iPhone5) {
        height+=88;
    }
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 38,320,height)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.separatorColor=[UIColor colorWithRed:228.0/255 green:228.0/255 blue:228.0/255 alpha:1.0];
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:[Utility customButton:self action:@selector(viewLogAction:) normalImage:@"qz_looklog_normal.png" lightedImage:@"qz_looklog_hover.png" frame:CGRectMake(204, 9, 104, 21) title:nil tag:22]];
    
    
     UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(backToPre) normalImage:@"navback_normal.png" lightedImage:@"navback_hover.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:10]];
    self.navigationItem.leftBarButtonItem=backBarButtonItem;
    
       UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(backToPre) normalImage:@"noti_save_normal.png" lightedImage:@"noti_save_hover.png" frame:CGRectMake(0, 0, 59, 30) title:nil tag:11]];
    self.navigationItem.rightBarButtonItem=rightBarButtonItem;
   
}

-(void)backToPre{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)loadModel{
    self.currentRow = -1;
    self.headViewArray = [[NSMutableArray alloc]init];
    for(int i = 0;i<self.dataArray.count ;i++){
        NSString *imageName=[[self.dataArray objectAtIndex:i] objectForKey:@"logo"];
		HeadView* headview = [[HeadView alloc] init];
        headview.delegate = self;
		headview.section = i;
        if (i==0) {
            headview.open=YES;
        }
        [headview.backBtn  setBackgroundImage:[Utility imageNamed:imageName] forState:UIControlStateNormal];
         [headview.backBtn  setBackgroundImage:[Utility imageNamed:imageName] forState:UIControlStateSelected];
        [headview.backBtn  setBackgroundImage:[Utility imageNamed:imageName] forState:UIControlStateHighlighted];
		[self.headViewArray addObject:headview];
	}
}



#pragma mark - TableViewdelegate&&TableViewdataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    HeadView* headView = [self.headViewArray objectAtIndex:indexPath.section];
    
    return headView.open?80:0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 29;
}


- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [self.headViewArray objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    HeadView* headView = [self.headViewArray objectAtIndex:section];
    return headView.open?[[[self.dataArray objectAtIndex:section] objectForKey:@"list"] count]:0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.headViewArray count];
}



- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentifier = @"bowenmanageViewCell";
    BowenmanageViewCell *cell =(BowenmanageViewCell*)[tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell =[[BowenmanageViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    switch (indexPath.section) {
        case 0:{
            cell.button1.frame=CGRectMake(205, 16, 50, 20);
           
            [cell.button1 setImage:[Utility imageNamed:@"qz_goedit_normal.png"] forState:UIControlStateNormal];
            [cell.button1 setImage:[Utility imageNamed:@"qz_goedit_hover.png"] forState:UIControlStateSelected];
            [cell.button1 setImage:[Utility imageNamed:@"qz_goedit_hover.png"] forState:UIControlStateHighlighted];
            
             cell.button2.frame=CGRectMake(260, 16, 50, 20);
            [cell.button2 setImage:[Utility imageNamed:@"qz_publish_normal.png"] forState:UIControlStateNormal];
            [cell.button2 setImage:[Utility imageNamed:@"qz_publish_hover.png"] forState:UIControlStateSelected];
            [cell.button2 setImage:[Utility imageNamed:@"qz_publish_hover.png"] forState:UIControlStateHighlighted];
            break;
        }
        case 1:{
            cell.button1.frame=CGRectMake(205, 16, 49, 20);
            cell.button2.frame=CGRectMake(260, 16, 49, 20);
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_hover.png"] forState:UIControlStateNormal];
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_normal.png"] forState:UIControlStateSelected];
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_normal.png"] forState:UIControlStateHighlighted];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_normal.png"] forState:UIControlStateNormal];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_normal.png"] forState:UIControlStateSelected];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_hover.png"] forState:UIControlStateHighlighted];
            break;
        }
        case 2:{
            cell.button1.frame=CGRectMake(205, 16, 49, 20);
            cell.button2.frame=CGRectMake(260, 16, 49, 20);
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_normal.png"] forState:UIControlStateNormal];
            [cell.button1 setImage:[Utility imageNamed:@"qz_zhiding_normal.png"] forState:UIControlStateSelected];
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_hover.png"] forState:UIControlStateHighlighted];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_normal.png"] forState:UIControlStateNormal];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_normal.png"] forState:UIControlStateSelected];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_hover.png"] forState:UIControlStateHighlighted];
            break;
        }
        case 3:{
            cell.button1 .frame=CGRectMake(205, 16, 49, 20);
            cell.button2.frame=CGRectMake(260, 16, 49, 20);
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_normal.png"] forState:UIControlStateNormal];
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_normal.png"] forState:UIControlStateSelected];
            [cell.button1  setImage:[Utility imageNamed:@"qz_zhiding_hover.png"] forState:UIControlStateHighlighted];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_hover.png"] forState:UIControlStateNormal];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_normal.png"] forState:UIControlStateSelected];
            [cell.button2 setImage:[Utility imageNamed:@"qz_hide_normal.png"] forState:UIControlStateHighlighted];
            break;
        }

    }
     HeadView *head = [self.headViewArray objectAtIndex:indexPath.row];
    if (head.open) {
        head.open = YES;
        UIImageView *imageView=(UIImageView*)[head viewWithTag:2000];
        imageView.image=[Utility imageNamed:@"quanziopen.png"];
    }
      cell.delegate=self;
      cell.indexPath=indexPath;
      cell.labelContent.text=@"一起看流星雨";
      cell.labelDetail.text=[NSString stringWithFormat:@"%@ 阅读:%@  评论:%@  收藏:  %@",@"2013-2-5 04:15",@"100",@"100",@"100"];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.currentRow = indexPath.row;
    [self.tableView reloadData];
}


#pragma mark - HeadViewdelegate
-(void)selectedWith:(HeadView *)view{
    self.currentRow = -1;
    if (view.open) {
        for(int i = 0;i<[self.headViewArray count];i++)
        {
            HeadView *head = [self.headViewArray objectAtIndex:i];
            head.open = NO;
            //[head.backBtn setBackgroundImage:[UIImage imageNamed:@"btn_momal"] forState:UIControlStateNormal];
             UIImageView *imageView=(UIImageView*)[head viewWithTag:2000];
            imageView.image=[Utility imageNamed:@"quanziFolder.png"];
        }
        [_tableView reloadData];
        return;
    }
    self.currentSection = view.section;
    [self reset];
    
}

//界面重置
- (void)reset
{
    for(int i = 0;i<[self.headViewArray count];i++)
    {
        HeadView *head = [self.headViewArray objectAtIndex:i];
          UIImageView *imageView=(UIImageView*)[head viewWithTag:2000];
        if(head.section == self.currentSection){
            head.open = YES;
            imageView.image=[Utility imageNamed:@"quanziopen.png"];
            
        }else {
            imageView.image=[Utility imageNamed:@"quanziFolder.png"];
            head.open = NO;
        }
        
    }
    [self.tableView reloadData];
}

//cell中按钮
-(void) buttonOfCellAction:(NSIndexPath*) indexpath andtag:(int) tag{
     NSLog(@"%d",tag);
}
//查看日志按钮
-(IBAction)viewLogAction:(id)sender{
    NSLog(@"%@",@"查看圈子日志");

}


- (void)dealloc{

}

@end
